/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.adapters;

import com.itgrail.pilot.domain.user.entity.MenuE;
import com.itgrail.pilot.domain.user.ports.MenuPort;
import com.itgrail.pilot.infrastructure.converter.MenuConverter;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.MenuMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.MenuDO;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.ddd.annotation.infrastructure.Adapter;

import java.util.List;


@Adapter
public class MenuAdapter implements MenuPort {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuConverter menuConverter;

    @Override
    public Long save(MenuE menuE) {
        MenuDO menuDO = menuConverter.entityToData(menuE);
        if (menuDO.getId() == null) {
            menuMapper.insert(menuDO);
        } else {
            menuMapper.updateById(menuDO);
        }
        return menuDO.getId();
    }

    @Override
    public MenuE findById(Long id) {
        MenuDO menuDO = menuMapper.selectById(id);
        return menuConverter.dataToEntity(menuDO);
    }

    @Override
    public List<MenuE> findMenusByParentId(Long parentId) {
        List<MenuDO> menuDOList = menuMapper.findMenusByParentId(parentId);
        return menuConverter.dataToEntity(menuDOList);
    }

    @Override
    public Integer deleteById(Long id) {
        MenuDO menuDO = new MenuDO();
        menuDO.setId(id);
        menuDO.setIsDeleted((byte) 1);
        return menuMapper.updateById(menuDO);
    }
}
